package com.huike.clues.service.impl;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysRoleDeptMapper;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.SysUserRoleMapper;
import com.huike.clues.result.RoleDeptTreeAjaxResult;
import com.huike.clues.service.ISysDeptService;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDeptDTO;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.DateUtils;
import com.sun.xml.bind.v2.TODO;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Desciption ISysDeptServiceImpl
 * @Author LUXIN
 * @Date 2023-10-16
 */
@Service
public class SysDeptServiceImpl implements ISysDeptService {

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysRoleDeptMapper sysRoleDeptMapper;

    /**
     * 获取部门列表
     *
     * @param sysDeptDTO
     * @return
     */
    @Override
    public List<SysDeptDTO> deptList(SysDeptDTO sysDeptDTO) {
        return sysDeptMapper.findList(sysDeptDTO);
    }

    /**
     * 获取部门下拉树列表
     *
     * @param sysDeptDTO
     * @return
     */
    @Override
    public List<TreeSelect> findDeptListBy(SysDeptDTO sysDeptDTO) {
        List<TreeSelect> treeSelectList = new ArrayList<>();
        // 获取起始的父起点集合
        TreeSelect treeSelect = sysDeptMapper.findListBy(sysDeptDTO);
        // 获取子列表集合，包含北京总公司和销售公司
        List<TreeSelect> childrenList = sysDeptMapper.findChildren(treeSelect.getId());
        for (TreeSelect children : childrenList) {
            Long deptId = children.getId();
            // 获取根据找出以deptId作为父id的子列表集合
            List<TreeSelect> childrenS = sysDeptMapper.findChildren(deptId);
            for (TreeSelect child : childrenS) {
                Long childId = child.getId();
                List<TreeSelect> childs = sysDeptMapper.findChildren(childId);
                child.setChildren(childs);
            }
            children.setChildren(childrenS);
        }
        treeSelect.setChildren(childrenList);
        treeSelectList.add(treeSelect);
        return treeSelectList;
    }

    /**
     * 新增部门
     *
     * @param dept
     */
    @Override
    public void addDept(SysDeptDTO dept) {
        // 暂时补全
        dept.setDelFlag("0");
        // 根据上级父部门的id找出再上级的部门id
        dept.setCreateTime(DateUtils.getNowDate());
        dept.setCreateBy("admin");
        sysDeptMapper.addDept(dept);
        // 获取部门id
        Long deptId = dept.getDeptId();
        // 使用部门id找到父部门id
        Long parentId = sysDeptMapper.findById(deptId);
        // 再根据parentId获取祖级列表
        String st = sysDeptMapper.findByIds(parentId);
        // 进行拼接
        String an = st + "," + parentId;
        dept.setAncestors(an);
        // 修改父列表
        sysDeptMapper.updateDept(dept);
    }

    /**
     * 修改部门
     *
     * @param dept
     */
    @Override
    public void updateDept(SysDeptDTO dept) {

        // 调用业务层，将前端传递过来的数据进行修改再加上修改人和修改时间，暂时补全
        dept.setUpdateTime(DateUtils.getNowDate());
        dept.setUpdateBy("admin");
        // 删除祖级列表
        // 获取部门id
        Long deptId = dept.getDeptId();
        // 使用部门id找到父部门id
        Long parentId = sysDeptMapper.findById(deptId);
        // 再根据parentId获取祖级列表
        String st = sysDeptMapper.findByIds(parentId);
        // 进行拼接
        String an = st + "," + parentId;
        dept.setAncestors(an);
        sysDeptMapper.updateDept(dept);

    }

    /**
     * 查询部门列表接口
     *
     * @param deptId
     * @return
     */
    @Override
    public List<SysDeptDTO> findDeptList(Integer deptId) {
        List<SysDeptDTO> sysDeptDTOList = new ArrayList<>();
        // 根据当前部门id查询部门信息
        SysDeptDTO sysDeptDTO = sysDeptMapper.findListById(deptId);
        // 根据父起点查询子节点内容，但是要排除本部门
        List<SysDeptDTO> deptList = sysDeptMapper.List(sysDeptDTO.getDeptId());
        Long dId = Long.valueOf(deptId);
        List<Long> idList = deptList.stream().map(SysDeptDTO::getDeptId).collect(Collectors.toList());
        for (int i = 0; i < idList.size(); i++) {
            if (dId.equals(idList.get(i))) {
                deptList.remove(i);
            }
        }
        // 再根据子集合查询下面的结点
        for (SysDeptDTO deptDTO : deptList) {
            Long dId2 = deptDTO.getDeptId();
            List<SysDeptDTO> list = sysDeptMapper.List(dId2);
            List<Long> ids = list.stream().map(SysDeptDTO::getDeptId).collect(Collectors.toList());
            for (int i = 0; i < ids.size(); i++) {
                if (dId.equals(ids.get(i))) {
                    list.remove(i);
                }
            }
            deptDTO.setChildren(list);
        }
        sysDeptDTO.setChildren(deptList);
        sysDeptDTOList.add(sysDeptDTO);
        return sysDeptDTOList;
    }

    /**
     * 获取对应部门的详细信息
     *
     * @param deptId
     * @return
     */
    @Override
    public SysDeptDTO findDept(Integer deptId) {
        return sysDeptMapper.findDept(deptId);
    }

    /**
     * 删除部门
     *
     * @param deptId
     */
    @Override
    public void deleteDept(Integer deptId) {
        // 如果有员工的话就不可以删除
        Integer count = sysUserMapper.findBy(deptId);
        if (count > 0) {
            throw new BaseException("该部门还有下属员工，不允许被删除");
        }
        // 如果下面还有部门也不可以删除
        count = sysDeptMapper.findBy(deptId);
        if (count > 0) {
            throw new BaseException("该部门还有下属部门，不允许被删除");
        }
        sysDeptMapper.deleteDept(deptId);
    }

    /**
     * 获取对应角色的部门数列表
     *
     * @param roleId
     * @return
     */
    @Override
    public RoleDeptTreeAjaxResult findRoleDeptTree(Long roleId) {
        RoleDeptTreeAjaxResult result = new RoleDeptTreeAjaxResult();

        List<Integer> checkedKeys = sysRoleDeptMapper.findByRoleId(roleId);


        List<TreeSelect> menus = sysDeptMapper.findChildrens(checkedKeys);
        for (TreeSelect treeSelect : menus) {

            List<TreeSelect> byParentId = sysDeptMapper.findChildren(treeSelect.getId());
            treeSelect.setChildren(byParentId);

            for (TreeSelect setCd : byParentId) {
                List<TreeSelect> child = sysDeptMapper.findChildren(setCd.getId());
                setCd.setChildren(child);
            }

        }

        result.setDepts(menus);
        result.setCheckedKeys(checkedKeys);
        result.setMsg("操作成功");

        return result;
    }
}
